﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main_Reports_Accounts_JournalBooks : System.Web.UI.Page
{
    Datautility du = new Datautility();
    AccountSettings ac = new AccountSettings();
    int firstrow = 0;
    string previousbill = string.Empty;
    double dblTotalDebit = 0;
    double dblTotalCredit = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["UserId"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }


        if (!IsPostBack)
        {
            lblcompname.Text = Session["CompName"].ToString();

            txtFromDate.Text = "01/04/" + Session["finyear"].ToString();
            txtToDate.Text = "31/03/" + (Convert.ToInt32(Session["finyear"].ToString()) + 1).ToString();
            hidFinyear.Value = Session["finyear"].ToString();

            getJournalBook(txtFromDate.Text, txtToDate.Text);
            grdDisplay.UseAccessibleHeader = true;
            grdDisplay.HeaderRow.TableSection = TableRowSection.TableHeader;
            grdDisplay.FooterRow.TableSection = TableRowSection.TableFooter;
        }

    }

    protected void btnShow_Click(object sender, EventArgs e)
    {

        getJournalBook(txtFromDate.Text, txtToDate.Text);

    }
    protected void getJournalBook(string strFromDate, string strToDate)
    {
        if (strFromDate != string.Empty && strToDate != string.Empty)
        {
            string[] str = strFromDate.Split('/');
            strFromDate = str[1] + "/" + str[0] + "/" + str[2];

            str = strToDate.Split('/');
            strToDate = str[1] + "/" + str[0] + "/" + str[2];

            lblHeading.Text = "Journal Books From " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strFromDate)) + " To " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strToDate));

            SqlParameter[] pr ={
                             new SqlParameter("@FromDate",strFromDate) ,
                            new SqlParameter("@ToDate",strToDate ),
                           new SqlParameter ("@TransactionTypeList","4")
                           };
            DataTable dt = du.getdatausingprocedure("USP_ACC_AccountBookVoucherDetails", pr);
            grdDisplay.DataSource = dt;
            grdDisplay.DataBind();

        }
    }
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[4].Text != string.Empty)
            {
                dblTotalDebit += Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text));
                if (Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text)) != 0)
                {

                    e.Row.Cells[4].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text))).ToString();
                }
                else
                {
                    e.Row.Cells[4].Text = string.Empty;
                }
            }
            if (e.Row.Cells[5].Text != string.Empty)
            {
                dblTotalCredit += Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text));


                if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) != 0)
                {

                    e.Row.Cells[5].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text))).ToString();
                }
                else
                {
                    e.Row.Cells[5].Text = string.Empty;
                }
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dv = ((DataRowView)e.Row.DataItem);
            if (previousbill == dv["TransactionNumber"].ToString())
            {
                if (grdDisplay.Rows[firstrow].Cells[2].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[2].RowSpan = 2;

                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[2].RowSpan += 1;
                }
                if (grdDisplay.Rows[firstrow].Cells[1].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[1].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[1].RowSpan += 1;
                }
                if (grdDisplay.Rows[firstrow].Cells[0].RowSpan == 0)
                {
                    grdDisplay.Rows[firstrow].Cells[0].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstrow].Cells[0].RowSpan += 1;

                }
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
                e.Row.Cells.RemoveAt(0);
            }
            else
            {
                e.Row.VerticalAlign = VerticalAlign.Middle;
                previousbill = dv["TransactionNumber"].ToString();
                firstrow = e.Row.RowIndex;

            }
            e.Row.Cells[2].Attributes.Add("border-right", "solid 1px #818181");
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[3].Text = "Total";
            e.Row.Cells[4].Text = ac.GetCurrency(dblTotalDebit).ToString();
            e.Row.Cells[5].Text = ac.GetCurrency(dblTotalCredit).ToString();
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }

    }
}
